#!/bin/bash
# This required jq and wget

DAY=$((60 * 60 * 24))


# Remove instances.json when it's older then 1 day
if [ -f "instances.json" ]; then
  NOW=$(date "+%s")
  THEN=$(date -r "instances.json" "+%s")
  DELTA=$((NOW - THEN))
  if [ $DELTA -gt $DAY ]; then
    echo "YES"
    rm "instances.json"
  fi
fi


# Get new instances.json
if [ ! -f "instances.json" ]; then
  wget "https://api.invidious.io/instances.json"
fi

# Process
while IFS= read -r line
do
  URL="${line:1:-1}"
  echo "// @include  https://$URL/*"
done <<< $(jq ".[][0]" instances.json | grep -v ".onion\|.i2p")
